<template>
  <el-dialog class="set_dialog" width="500px" title="设置活动" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <p class="text-center tip">您确定要设置该商品活动吗？</p>
    <el-form ref="ruleForm" class="ruleForm" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="商品活动" prop="reason">
        <el-select v-model="form.activity" placeholder="请选择" clearable>
          <el-option v-for="item in activityList" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { activityList, activityAdd } from '@/api/goods'
export default {
  props: {
    actGoodsId: {
      type: String,
      default: ''
    },
    actGoods:{
      type:Object,
      default:()=>({})
    }
  },
  data() {
    return {
      form: {
        activity: ''
      },
      rules: {
        activity: [
          { required: true, message: '请选择商品活动', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      activityList: []
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
          for (const key in this.form) {
            if(key==='activity'){
              this.form.activity=this.actGoods.activity_id
            }
          }
          // if (res.activity_type) {
          //   this.form.activity = res.activity_type.split(',')
          // }
      }
    }
  },
  created(){
    activityList().then(res=>{
      this.activityList=res
    })
    console.log(this.actGoodsId);
  },
  methods: {
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const params = {
            goods_id: this.actGoodsId,
            activity_id: this.form.activity
          }
          activityAdd(this.actGoodsId, this.form.activity).then(res => {
            this.dialogVisible = false
            this.$message({
              message: '设置成功',
              type: 'success'
            })
            this.$emit('getList', true)
          })
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tip {
  line-height: 35px;
  margin-bottom: 10px;
}
</style>
